import type { DataTableColumns, DropdownOption } from 'naive-ui'
import type { CreateColumsOptions } from '@/types/fcform'
import { NDropdown } from 'naive-ui'
import roleApi from '@/service/api/setting/role.ts'

export const createColums = ({ handDel, action }: CreateColumsOptions): DataTableColumns => {
  return [
    {
      type: 'selection',
      options: ['all', 'none'],
      disabled(row) {
        return row.roleCode === 'ROLE_ADMIN'
      },
    },
    {
      title: '角色名称',
      key: 'roleName',
    },
    {
      title: '角色Code',
      key: 'roleCode',
    },
    {
      title: '操作',
      key: 'total',
      align: 'center',
      width: 120,
      render(row) {
        const arr = []
        if (commonUnit.auth('sys_role_edit')) {
          const editBtn = fastUnit.createBtn('编辑', {
            type: 'warning',
            onClick: () => action.default(row),
          })
          arr.push(editBtn)
        }
        const moreBtnOptions: DropdownOption[] = []
        if (commonUnit.auth('sys_role_perm')) {
          moreBtnOptions.push({
            label: '授权',
            key: 'role',
          })
        }
        if (commonUnit.auth('sys_role_del')) {
          moreBtnOptions.push({
            label: () =>
              h(
                'div',
                {
                  style: {
                    color: 'red',
                  },
                },
                '删除',
              ),
            key: 'del',
          })
        }
        const moreBtn = h(
          NDropdown,
          {
            trigger: 'hover',
            options: moreBtnOptions,
            onSelect: (e: string) => {
              switch (e) {
                case 'role':
                  roleApi.getAuth(row.roleId).then(({ data }) => {
                    action.auth({
                      roleId: row.roleId,
                      menuIds: data,
                    })
                  })
                  //
                  break
                case 'del':
                  handDel(row)
                  break
              }
            },
          },
          {
            default: () => fastUnit.createBtn('更多'),
          },
        )
        if (moreBtnOptions.length > 0) {
          arr.push(moreBtn)
        }
        return h('div', arr)
      },
    },
  ]
}
